<?php
/**
 * Created by PhpStorm.
 * User: changshuiwang
 * Date: 2016/7/25
 * Time: 16:35
 */
namespace backend\controllers;

use backend\config\AuthDef;
use common\models\Common;
use common\models\Teacher;
use common\service\AuthSrv;
use Yii;
use yii\base\Controller;
use yii\data\ActiveDataProvider;
use yii\data\Sort;

class TeaController extends Controller
{

    public function beforeAction($action)
    {
        $action = Yii::$app->controller->id.'/'.Yii::$app->controller->action->id;

        if(\Yii::$app->user->can($action)){
            return true;
        }else{
            //$id=Yii::$app->user->getId();
            if(!Yii::$app->user->isGuest){
                echo"<script>alert('对不起，您现在还没获此操作的权限');
                    window.location.href='/site/index';</script>";
            }else{
                echo"<script>alert('对不起，您现在还没获此操作的权限');
                    window.location.href='/site/login';</script>";
                //$this->redirect("/site/login");
            }
            //return false;
            //throw new Exception("..");
        }
    }

    public function actionManage()
    {
        $query=Teacher::getAllTeacher();
        $teacher = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                'pageSize' => 10,
            ],
            'sort'=>new Sort([
                'attributes' => [
                    'id',
                ],
            ]),
        ]);
        $this->getView()->title="管理教师";
        return $this->render('manage',['teacher'=>$teacher]);
    }

    public function actionDelete()
    {
        $id=$_GET['id'];
        $teacher=Teacher::find()->where(['id'=>$id])->andWhere(['type'=>2])
            ->andWhere(['status'=>10])->one();
        if($teacher){
            $teacher->setScenario('delete');
            $teacher->status=0;
            $success=$teacher->save();
            if($success){
                return json_encode(array('status'=>'success'));
            }else{
                return json_encode(array('status'=>'fail','error'=>$success));
            }
        }else{
            return json_encode(array('status'=>'fail','error'=>'参数不合法'));
        }
    }

    public function actionAdd()
    {
        if(Yii::$app->request->isPost){
            if($_POST['password_hash']!==$_POST['password_hash2']){
                return json_encode(array('status'=>'fail','error'=>'两次密码不一致'));
            }

            if(isset($_COOKIE['validateCode'])){
                $validateCode=Common::authcode($_COOKIE['validateCode'],'DECODE');
                if(strtolower($validateCode)!==strtolower($_POST['validateCode'])){
                    return json_encode(array('status'=>'fail','error'=>'图片验证码错误'.$validateCode));
                }
            }else{
                return json_encode(array('status'=>'fail','error'=>'图片验证码失效'));
            }
            
            $teacher=new Teacher();
            $teacher->setScenario('add');
            $_POST['password_hash']=Yii::$app->security->generatePasswordHash($_POST['password_hash']);
            $teacher->setAttributes($_POST);
            $errors=$teacher->validate();
            if($errors){
                $teacher->type=2;
                $success=$teacher->save();
                if($success){
                    $id=Yii::$app->db->getLastInsertID();
                    AuthSrv::assignAuth($teacher->type,$id);
                    return json_encode(array('status'=>'success'));
                }else{
                    return json_encode(array('status'=>'fail','error'=>$success));
                }
            }else{
                $errors=$teacher->getErrors();
                while(is_array($errors)){
                    $errors=current($errors);
                }
                return json_encode(array('status'=>"error",'error'=>$errors));
            }
        }else{
            $this->getView()->title="添加教师";
            return $this->renderPartial('add');
        }
    }
}